/*
███████╗██╗   ██╗ ██████╗██╗  ██╗
██╔════╝██║   ██║██╔════╝██║ ██╔╝
█████╗  ██║   ██║██║     █████╔╝ 
██╔══╝  ██║   ██║██║     ██╔═██╗ 
██║     ╚██████╔╝╚██████╗██║  ██╗
╚═╝      ╚═════╝  ╚═════╝╚═╝  ╚═╝
*/
package dao;


import common.JavaBean.Review;
import common.utils.DBUtilsV2;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ReviewDao {

    /* 用户提交/修改评价 */
    public void userSubmitReview(int userId, int orderId, int rating, String content) throws SQLException {
        String sql = "{CALL sp_user_submit_review(?, ?, ?, ?)}";
        try (Connection conn = DBUtilsV2.open();
             CallableStatement cstmt = conn.prepareCall(sql)) {
            cstmt.setInt(1, userId);
            cstmt.setInt(2, orderId);
            cstmt.setInt(3, rating);
            cstmt.setString(4, content);
            cstmt.execute();
        }
    }

    /* 管理员打回评价 */
    public void adminRejectReview(int reviewId) throws SQLException {
        String sql = "{CALL sp_admin_reject_review(?)}";
        try (Connection conn = DBUtilsV2.open();
             CallableStatement cstmt = conn.prepareCall(sql)) {
            cstmt.setInt(1, reviewId);
            cstmt.execute();
        }
    }

    /* 商品页查询评价 */
    public ResultSet queryReviews(int productId, java.sql.Timestamp start,
                                  java.sql.Timestamp end, Integer minRating) throws SQLException {
        String sql = "{CALL sp_query_reviews_for_product(?, ?, ?, ?)}";
        Connection conn = DBUtilsV2.open();
        CallableStatement cstmt = conn.prepareCall(sql);
        cstmt.setInt(1, productId);
        cstmt.setTimestamp(2, start);
        cstmt.setTimestamp(3, end);
        if (minRating == null) cstmt.setNull(4, Types.TINYINT); else cstmt.setInt(4, minRating);
        return cstmt.executeQuery();
    }

    /* 商品页评价汇总 */
    public ResultSet getReviewSummary(int productId) throws SQLException {
        String sql = "{CALL sp_get_review_summary(?)}";
        Connection conn = DBUtilsV2.open();
        CallableStatement cstmt = conn.prepareCall(sql);
        cstmt.setInt(1, productId);
        return cstmt.executeQuery();
    }
    /* 管理员后台多模态查询评价（含用户/订单/商品维度） */
    public ResultSet queryReviewsAdmin(Integer reviewId, Integer userId, Integer productId,
                                       Integer orderId, Integer status, java.sql.Timestamp start, java.sql.Timestamp end) throws SQLException {
        String sql = "{CALL sp_query_reviews_admin(?, ?, ?, ?, ?, ?, ?)}";
        Connection conn = DBUtilsV2.open();
        CallableStatement cstmt = conn.prepareCall(sql);
        if (reviewId == null) cstmt.setNull(1, Types.INTEGER); else cstmt.setInt(1, reviewId);
        if (userId == null) cstmt.setNull(2, Types.INTEGER); else cstmt.setInt(2, userId);
        if (productId == null) cstmt.setNull(3, Types.INTEGER); else cstmt.setInt(3, productId);
        if (orderId == null) cstmt.setNull(4, Types.INTEGER); else cstmt.setInt(4, orderId);
        if (status == null) cstmt.setNull(5, Types.TINYINT); else cstmt.setInt(5, status);
        cstmt.setTimestamp(6, start);
        cstmt.setTimestamp(7, end);
        return cstmt.executeQuery(); // 调用者负责关闭
    }
}
